package com.ynkbny.tasks;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ynkbny.aspect.TaskOperationLog;
import com.ynkbny.domain.DataCodeAnalyzeMapping;
import com.ynkbny.domain.dto.TEpDataCollectConfigureDTO;
import com.ynkbny.domain.dto.TEpTotalEnergyDTO;
import com.ynkbny.service.BaseJob;
import com.ynkbny.service.DatacodeAnalyzeMappingService;
import com.ynkbny.service.TEpDataCollectConfigureService;
import com.ynkbny.service.TEpTotalEnergyService;
import lombok.var;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author YY
 * @Date 2023/11/30
 */
@Component
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@ConditionalOnProperty(name = "schedule.enabled", havingValue = "false")
public class GetUploadDataByAnalyzeMap implements BaseJob {

    private static final Logger log = LoggerFactory.getLogger(GetUploadDataByAnalyzeMap.class);

    @Resource
    private DatacodeAnalyzeMappingService datacodeAnalyzeMappingService;

    @Resource
    private TEpTotalEnergyService totalEnergyService;

    @Resource
    private TEpDataCollectConfigureService dataCollectConfigureService;

    /**
     * 通过映射表获取上报数据
     */
    public void saveEnergyData() {
        // 获取数据编码分析映射列表
        List<DataCodeAnalyzeMapping> list = datacodeAnalyzeMappingService.list();
        // 如果列表不为空，则获取数据映射
        if (list != null && !list.isEmpty()) {
            Map<String, Double> dataMap = datacodeAnalyzeMappingService.getDataMap(list);
            // 根据id获取配置列表
            List<TEpDataCollectConfigureDTO> configureDTOList = dataCollectConfigureService.listByIds(list.stream().map(DataCodeAnalyzeMapping::getId).collect(Collectors.toList()));
            // 根据数据映射和配置列表构建上传数据列表
            List<TEpTotalEnergyDTO> uploadDataList = datacodeAnalyzeMappingService.buildUploadData(dataMap, configureDTOList);
            // 如果上传数据列表不为空，则保存上传数据
            if (uploadDataList != null && !uploadDataList.isEmpty()) {
                uploadDataList.forEach(temp -> {
                    var lambdaQueryWrapper = new LambdaQueryWrapper<TEpTotalEnergyDTO>();
                    lambdaQueryWrapper.eq(TEpTotalEnergyDTO::getEnterpriseCode, temp.getEnterpriseCode())
                            .eq(TEpTotalEnergyDTO::getDataCode,temp.getDataCode())
                            .eq(TEpTotalEnergyDTO::getStatDate,temp.getStatDate());
                    totalEnergyService.saveOrUpdate(temp, lambdaQueryWrapper);
                    // 打印日志
                    log.info("保存上传数据：{}", temp);
                });
            }
        }
    }

    @Override
    @TaskOperationLog(description = "通过映射表获取上报数据")
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        this.saveEnergyData();
    }
}